





var xmlFileName = "file:///android_asset/test.xml";
window.onload = function(){
	createSplash();
	var jQueryScript = importScript("file:///android_asset/templates/scripts/jquery-1.8.2.min.js");
	jQueryScript.onload = function(){
		showSplash(false);
		viewXmlTree();
	}
};
function importScript(url){
	var ga = document.createElement('script'); ga.type = 'text/javascript'; 
    ga.async = true;
	ga.src = url;
	document.getElementsByTagName('head')[0].appendChild(ga);
   	return ga;
}
function getUrlVars(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++){
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
function createSplash(){
	var loading = document.createElement("div");
	loading.className ='loading';
	loading.style.textAlign = "center";
	var img = document.createElement("img");
	img.src = "file:///android_asset/templates/scripts/resources/loader.gif";
	loading.appendChild(img);
	document.getElementsByTagName("body")[0].appendChild(loading);
}
function showSplash(show){
	if(show){
		$(".loading").show();
	}
	else{
		$(".loading").hide();
	}
}


function viewXmlTree(){
		
 
	var result = $("<div class='loadXML'></div>").load(xmlFileName,function(){
		$(this).insertAfter($("body"));
		var allElements = $(this).find($("*"));
		var count = 0;
		allElements.each(function(){
			readTag($(this));
		});
 		$("#page").fadeIn(100, function(){
 			
 			cleanHtmlTemplate();
 			
 		});
 		
	});
}	
function cleanHtmlTemplate(){
	$(".loading").remove();
	$(".loadXML").remove();
	$("script").remove();
}
function readTag(tag){
	if(tag != undefined){
		//console.log(tag.prop("nodeName").toLowerCase());
		var findNewTagHtml = $("." + tag.prop("nodeName").toLowerCase()).not(".fixedHtml");
		var findCloneTagHtml =  $("." + tag.prop("nodeName").toLowerCase()+ ".fixedHtml").last();
		//console.log(findNewTagHtml, findCloneTagHtml);
						
		if(findNewTagHtml.length){
			findNewTagHtml.text(tag.attr("value"));
			findNewTagHtml.addClass("fixedHtml");
			tag.addClass("fixesXml");
		}
		else if(findCloneTagHtml.length ){
			var pasteNewTag = findCloneTagHtml.clone();
			pasteNewTag.text(tag.attr("value"));
			pasteNewTag.find("*").removeClass("fixedHtml");
			pasteNewTag.addClass("fixedHtml");
			pasteNewTag.insertAfter(findCloneTagHtml);
		}
	}
}